package test;

import java.sql.Statement;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet_OriGood extends HttpServlet implements Servlet {
	public LoginServlet_OriGood() {
		// TODO Auto-generated constructor stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		String result = "";
		//
		// response.sendRedirect ( "index.jsp" );
		String sUserName = request.getParameter("txtUserName");
		if (sUserName == "" || sUserName == null || sUserName.length() > 20) {
			try {
				result = "oneone";
				request.setAttribute("ErrorUserName", result);
				response.sendRedirect("login.jsp");
			} catch (Exception e) {
			}
		}

		String sPasswd = request.getParameter("txtPassword");
		if (sPasswd == "" || sPasswd == null || sPasswd.length() > 20) {
			try {
				result = "two";
				request.setAttribute("ErrorPassword", result);
				response.sendRedirect("login.jsp");
			} catch (Exception e) {
			}
		}

		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
			System.out.println("ClassNotFoundException");
		}

		String url = "jdbc:postgresql://localhost:5432/postgres";

		java.sql.Connection connection = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			connection = DriverManager.getConnection(url, "tao", "root");
			stmt = connection.createStatement();
			//

			String sql = "select * from userinfo where username='" + sUserName
					+ "' and password = '" + sPasswd + "'";
			rs = stmt.executeQuery(sql);//

		} catch (SQLException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
		try {
			if (rs.next())

			{

				request.getSession().setAttribute("UserName", sUserName);
				response.sendRedirect("login_success.jsp");
			} else {

				// response.sendRedirect ( "MyJsp.jsp" );

				response.sendRedirect("login_failure.jsp");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
		try {
			if (null != rs) {
				rs.close();
			}
			if (null != stmt) {
				stmt.close();
			}
			if (null != connection) {
				connection.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}

	/**
  *
  */
	private static final long serialVersionUID = 1L;
}
